/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Table 1, 2, 3, 4 and A3
Input for Table 6, Figure A4 and Figures in appendix C
=========================================================================*/

local rd=1
local years=1
local het=1
local 2sls=1
local heterogeneities=1
********************************************************************************

use "${data}/data_RD", clear
keep if icfes_per==20142
glo cond_saber11	"eligible_sisben==1"
glo cond_sisben		"eligible_saber11==1"

fasterxtile wage0_8_ptile=wage0_8, nq(100)
gen wage0_8_top1=(wage0_8_ptile==100)
gen wage0_8_top25=(wage0_8_ptile>75 & wage0_8_ptile!=.)

fasterxtile wage0_9_ptile=wage0_9, nq(100)
gen wage0_9_top1=(wage0_9_ptile==100)
gen wage0_9_top25=(wage0_9_ptile>75 & wage0_9_ptile!=.)

foreach o in distancia_0 distancia_1 distancia_2 distancia_3 distancia_4 distancia_5 distancia_6{
	foreach w in hq lq pri pub hq_pri hq_pub lq_pri lq_pub{
		gen `o'_`w'=`o' if `w'==1
	}
}

cap drop access_0_stem access_0_stem_plus access_0_arts access_0_social
gen access_0_stem=(access_0==1 & stem==1)
gen access_0_stem_plus=(access_0==1 & stem_plus==1)
gen access_0_arts=(access_0==1 & arts==1)
gen access_0_social=(access_0==1 & social==1)

gen access_6_stem=(access_6==1 & stem==1)
gen access_6_stem_plus=(access_6==1 & stem_plus==1)
gen access_6_arts=(access_6==1 & arts==1)
gen access_6_social=(access_6==1 & social==1)

********************************************************************************
*** REDUCED-FORM
********************************************************************************

if `rd'{

global outcomes beneficiary_spp access_0 access_1 access_2 access_3 access_4 access_5 access_6 access_0_hq access_0_lq access_0_pri access_0_pub access_0_hq_pri access_0_hq_pub access_0_lq_pri access_0_lq_pub access_1_hq access_1_lq access_1_pri access_1_pub access_1_hq_pri access_1_hq_pub access_1_lq_pri access_1_lq_pub access_2_hq access_2_lq access_2_pri access_2_pub access_2_hq_pri access_2_hq_pub access_2_lq_pri access_2_lq_pub access_3_hq access_3_lq access_3_pri access_3_pub access_3_hq_pri access_3_hq_pub access_3_lq_pri access_3_lq_pub access_4_hq access_4_lq access_4_pri access_4_pub access_4_hq_pri access_4_hq_pub access_4_lq_pri access_4_lq_pub access_5_hq access_5_lq access_5_pri access_5_pub access_5_hq_pri access_5_hq_pub access_5_lq_pri access_5_lq_pub access_6_hq access_6_lq access_6_pri access_6_pub access_6_hq_pri access_6_hq_pub access_6_lq_pri access_6_lq_pub access_pro_0 access_pro_1 access_pro_2 access_pro_3 access_pro_4 access_pro_5 access_pro_6 access_tyt_0 access_tyt_1 access_tyt_2 access_tyt_3 access_tyt_4 access_tyt_5 access_tyt_6 dropout_inm dropout_inm_hq dropout_inm_lq dropout_inm_pri dropout_inm_pub dropout_inm_hq_pri dropout_inm_hq_pub dropout_inm_lq_pri dropout_inm_lq_pub dropout_inm_pro dropout_inm_pro_hq dropout_inm_pro_lq dropout_inm_tyt dropout_inm_tyt_hq dropout_inm_tyt_lq years_study graduation_snies graduation_snies_inm graduation_snies_pro graduation_snies_pro_inm graduation_snies_tyt graduation_snies_tyt_inm graduation_exam graduation_exam_hq graduation_exam_lq graduation_exam_pri graduation_exam_pub graduation_exam_hq_pri graduation_exam_hq_pub graduation_exam_lq_pri graduation_exam_lq_pub graduation_exam_inm graduation_exam_inm_hq graduation_exam_inm_lq graduation_exam_inm_pri graduation_exam_inm_pub graduation_exam_inm_hq_pri graduation_exam_inm_hq_pub graduation_exam_inm_lq_pri graduation_exam_inm_lq_pub graduation_exam_pro graduation_exam_pro_hq graduation_exam_pro_lq graduation_exam_pro_pri graduation_exam_pro_pub graduation_exam_pro_hq_pri graduation_exam_pro_hq_pub graduation_exam_pro_lq_pri graduation_exam_pro_lq_pub graduation_exam_pro_inm graduation_exam_pro_inm_hq graduation_exam_pro_inm_lq graduation_exam_pro_inm_pri graduation_exam_pro_inm_pub graduation_exam_pro_inm_hq_pri graduation_exam_pro_inm_hq_pub graduation_exam_pro_inm_lq_pri graduation_exam_pro_inm_lq_pub graduation_exam_tyt graduation_exam_tyt_hq graduation_exam_tyt_lq graduation_exam_tyt_pri graduation_exam_tyt_pub graduation_exam_tyt_hq_pri graduation_exam_tyt_hq_pub graduation_exam_tyt_lq_pri graduation_exam_tyt_lq_pub graduation_exam_tyt_inm graduation_exam_tyt_inm_hq graduation_exam_tyt_inm_lq graduation_exam_tyt_inm_pri graduation_exam_tyt_inm_pub graduation_exam_tyt_inm_hq_pri graduation_exam_tyt_inm_hq_pub graduation_exam_tyt_inm_lq_pri graduation_exam_tyt_inm_lq_pub graduation_snies_stem graduation_snies_stem_plus graduation_snies_arts graduation_snies_social graduation_snies_pro_stem graduation_snies_pro_stem_plus graduation_snies_pro_arts graduation_snies_pro_social graduation_snies_tyt_stem graduation_snies_tyt_stem_plus graduation_snies_tyt_arts graduation_snies_tyt_social graduation_exam_stem graduation_exam_stem_plus graduation_exam_arts graduation_exam_social graduation_exam_other graduation_exam_pro_stem graduation_exam_pro_stem_plus graduation_exam_pro_arts graduation_exam_pro_social graduation_exam_pro_other graduation_exam_tyt_stem graduation_exam_tyt_stem_plus graduation_exam_tyt_arts graduation_exam_tyt_social graduation_exam_tyt_other time_graduation_exam time_graduation_exam_hq time_graduation_exam_lq time_graduation_exam_pri time_graduation_exam_pub time_graduation_exam_hq_pri time_graduation_exam_hq_pub time_graduation_exam_lq_pri time_graduation_exam_lq_pub time_graduation_exam_inm time_graduation_exam_inm_hq time_graduation_exam_inm_lq time_graduation_exam_inm_pri time_graduation_exam_inm_pub time_graduation_exam_inm_hq_pri time_graduation_exam_inm_hq_pub time_graduation_exam_inm_lq_pri time_graduation_exam_inm_lq_pub time_graduation_pro time_graduation_pro_hq time_graduation_pro_lq time_graduation_pro_pri time_graduation_pro_pub time_graduation_pro_hq_pri time_graduation_pro_hq_pub time_graduation_pro_lq_pri time_graduation_pro_lq_pub time_graduation_pro_inm time_graduation_pro_inm_hq time_graduation_pro_inm_lq time_graduation_pro_inm_pri time_graduation_pro_inm_pub time_graduation_pro_inm_hq_pri time_graduation_pro_inm_hq_pub time_graduation_pro_inm_lq_pri time_graduation_pro_inm_lq_pub time_graduation_tyt time_graduation_tyt_hq time_graduation_tyt_lq time_graduation_tyt_pri time_graduation_tyt_pub time_graduation_tyt_hq_pri time_graduation_tyt_hq_pub time_graduation_tyt_lq_pri time_graduation_tyt_lq_pub time_graduation_tyt_inm time_graduation_tyt_inm_hq time_graduation_tyt_inm_lq time_graduation_tyt_inm_pri time_graduation_tyt_inm_pub time_graduation_tyt_inm_hq_pri time_graduation_tyt_inm_hq_pub time_graduation_tyt_inm_lq_pri time_graduation_tyt_inm_lq_pub score_saberpro score_saberpro_hq score_saberpro_lq score_saberpro_pri score_saberpro_pub score_saberpro_hq_pri score_saberpro_hq_pub score_saberpro_lq_pri score_saberpro_lq_pub score_saberpro_inm score_saberpro_inm_hq score_saberpro_inm_lq score_saberpro_inm_pri score_saberpro_inm_pub score_saberpro_inm_hq_pri score_saberpro_inm_hq_pub score_saberpro_inm_lq_pri score_saberpro_inm_lq_pub score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 score_sabertyt score_sabertyt_hq score_sabertyt_lq score_sabertyt_pri score_sabertyt_pub score_sabertyt_hq_pri score_sabertyt_hq_pub score_sabertyt_lq_pri score_sabertyt_lq_pub score_sabertyt_inm score_sabertyt_inm_hq score_sabertyt_inm_lq score_sabertyt_inm_pri score_sabertyt_inm_pub score_sabertyt_inm_hq_pri score_sabertyt_inm_hq_pub score_sabertyt_inm_lq_pri score_sabertyt_inm_lq_pub postgraduate work_0 work_1 work_2 work_3 work_4 work_5 work_6 work_7 work_8 time_first_job time_first_job_exam wage0_0 wage0_1 wage0_2 wage0_3 wage0_4 wage0_5 wage0_6 wage0_7 wage0_8 ln_wage_0 ln_wage_1 ln_wage_2 ln_wage_3 ln_wage_4 ln_wage_5 ln_wage_6 ln_wage_7 ln_wage_8 mw_0 mw_1 mw_2 mw_3 mw_4 mw_5 mw_6 mw_7 mw_8 distancia_0 distancia_0_hq distancia_0_lq distancia_0_pri distancia_0_pub distancia_0_hq_pri distancia_0_hq_pub distancia_0_lq_pri distancia_0_lq_pub distancia_1 distancia_1_hq distancia_1_lq distancia_1_pri distancia_1_pub distancia_1_hq_pri distancia_1_hq_pub distancia_1_lq_pri distancia_1_lq_pub distancia_2 distancia_2_hq distancia_2_lq distancia_2_pri distancia_2_pub distancia_2_hq_pri distancia_2_hq_pub distancia_2_lq_pri distancia_2_lq_pub distancia_3 distancia_3_hq distancia_3_lq distancia_3_pri distancia_3_pub distancia_3_hq_pri distancia_3_hq_pub distancia_3_lq_pri distancia_3_lq_pub distancia_4 distancia_4_hq distancia_4_lq distancia_4_pri distancia_4_pub distancia_4_hq_pri distancia_4_hq_pub distancia_4_lq_pri distancia_4_lq_pub distancia_5 distancia_5_hq distancia_5_lq distancia_5_pri distancia_5_pub distancia_5_hq_pri distancia_5_hq_pub distancia_5_lq_pri distancia_5_lq_pub distancia_6 distancia_6_hq distancia_6_lq distancia_6_pri distancia_6_pub distancia_6_hq_pri distancia_6_hq_pub distancia_6_lq_pri distancia_6_lq_pub cambio_programa quarters_worked wage0_8_ptile wage0_8_top1 wage0_8_top25 work_9 mw_9 wage0_9 ln_wage_9 wage0_9_ptile wage0_9_top1 wage0_9_top25 access_0_stem access_0_stem_plus access_0_arts access_0_social access_6_stem access_6_stem_plus access_6_arts access_6_social

local NO: word count of $outcomes
local NO=`NO'-1
matrix r2014=J(`NO',34,.)

local i=1
foreach o of global outcomes{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=17
		
	cap rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) 
	if _rc==0{
	local bw=e(h_l)
	cap drop kw
	gen kw = ( (( (`bw') - abs(running_`r') ) /(`bw')) * (abs(running_`r') < (`bw')) )
	matrix r2014[`i',`=`j'+1']=e(tau_cl)
	matrix r2014[`i',`=`j'+2']=e(tau_bc)
	matrix r2014[`i',`=`j'+3']=e(se_tau_cl)
	matrix r2014[`i',`=`j'+4']=e(se_tau_rb)
	matrix r2014[`i',`=`j'+5']=e(ci_r_rb)
	matrix r2014[`i',`=`j'+6']=e(ci_l_rb)
	matrix r2014[`i',`=`j'+7']=e(tau_cl_l)
	matrix r2014[`i',`=`j'+8']=e(tau_bc_l)
	matrix r2014[`i',`=`j'+9']=e(pv_cl)
	matrix r2014[`i',`=`j'+10']=e(pv_rb)
	matrix r2014[`i',`=`j'+11']=e(h_l)
	matrix r2014[`i',`=`j'+12']=e(b_l)
	matrix r2014[`i',`=`j'+13']=e(N_h_l)
	matrix r2014[`i',`=`j'+14']=e(N_h_r)
	matrix r2014[`i',`=`j'+15']=e(N_b_l)
	matrix r2014[`i',`=`j'+16']=e(N_b_r)
	matrix r2014[`i',`=`j'+17']=e(N)
	}
	else {
		dis as error "`o' `r' could not be run"
	}
	}
	local i=1+`i'
}

}

********************************************************************************
*** ESTIMATION BY YEARS AFTER HS
********************************************************************************

if `years'{
	
global outcomes_2 access_0 access_1 access_2 access_3 access_4 access_5 access_6 access_pro_0 access_pro_1 access_pro_2 access_pro_3 access_pro_4 access_pro_5 access_pro_6 access_tyt_0 access_tyt_1 access_tyt_2 access_tyt_3 access_tyt_4 access_tyt_5 access_tyt_6 enrolled_1 enrolled_2 enrolled_3 enrolled_4 enrolled_5 enrolled_6 graduation_exam_1 graduation_exam_2 graduation_exam_3 graduation_exam_4 graduation_exam_5 graduation_exam_6 graduation_exam_7 graduation_exam_pro_3 graduation_exam_pro_4 graduation_exam_pro_5 graduation_exam_pro_6 graduation_exam_pro_7 graduation_exam_tyt_1 graduation_exam_tyt_2 graduation_exam_tyt_3 graduation_exam_tyt_4 graduation_exam_tyt_5 graduation_exam_tyt_6 graduation_exam_tyt_7 graduation_snies_1 graduation_snies_2 graduation_snies_3 graduation_snies_4 graduation_snies_5 graduation_snies_6 graduation_snies_pro_4 graduation_snies_pro_5 graduation_snies_pro_6 graduation_snies_tyt_1 graduation_snies_tyt_2 graduation_snies_tyt_3 graduation_snies_tyt_4 graduation_snies_tyt_5 graduation_snies_tyt_6 score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 work_0 work_1 work_2 work_3 work_4 work_5 work_6 work_7 work_8 wage0_0 wage0_1 wage0_2 wage0_3 wage0_4 wage0_5 wage0_6 wage0_7 wage0_8 ln_wage_0 ln_wage_1 ln_wage_2 ln_wage_3 ln_wage_4 ln_wage_5 ln_wage_6 ln_wage_7 ln_wage_8 mw_0 mw_1 mw_2 mw_3 mw_4 mw_5 mw_6 mw_7 mw_8 work_9 mw_9 wage0_9 ln_wage_9

local NO: word count of $outcomes_2
local NO=`NO'-1
matrix ry2014=J(`NO',34,.)
local i=1
foreach o of global outcomes_2{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=17
	
	rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) 
	matrix ry2014[`i',`=`j'+1']=e(tau_cl)
	matrix ry2014[`i',`=`j'+2']=e(tau_bc)
	matrix ry2014[`i',`=`j'+3']=e(se_tau_cl)
	matrix ry2014[`i',`=`j'+4']=e(se_tau_rb)
	matrix ry2014[`i',`=`j'+5']=e(ci_r_rb)
	matrix ry2014[`i',`=`j'+6']=e(ci_l_rb)
	matrix ry2014[`i',`=`j'+7']=e(tau_cl_l)
	matrix ry2014[`i',`=`j'+8']=e(tau_bc_l)
	matrix ry2014[`i',`=`j'+9']=e(pv_cl)
	matrix ry2014[`i',`=`j'+10']=e(pv_rb)
	matrix ry2014[`i',`=`j'+11']=e(h_l)
	matrix ry2014[`i',`=`j'+12']=e(b_l)
	matrix ry2014[`i',`=`j'+13']=e(N_h_l)
	matrix ry2014[`i',`=`j'+14']=e(N_h_r)
	matrix ry2014[`i',`=`j'+15']=e(N_b_l)
	matrix ry2014[`i',`=`j'+16']=e(N_b_r)
	matrix ry2014[`i',`=`j'+17']=e(N)
	}
	local i=1+`i'
}

}
********************************************************************************
*** ESTIMATION FIX BW BY HETEROGENEITIES
********************************************************************************

if `het'{

foreach o of varlist score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 {
	foreach w in hq lq pri pub hq_pri hq_pub lq_pri lq_pub{
		gen `o'_`w'=`o' if `w'==1
	}
}

global outcomes_3 access_0 access_1 access_2 access_3 access_4 access_5 access_6 graduation_exam graduation_exam_inm graduation_exam_pro graduation_exam_pro_inm graduation_exam_tyt graduation_exam_tyt_inm graduation_snies graduation_snies_inm graduation_snies_pro graduation_snies_pro_inm graduation_snies_tyt graduation_snies_tyt_inm score_saberpro score_saberpro_inm score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 score_sabertyt score_sabertyt_inm

local NO: word count of $outcomes_3
local NO=(`NO'-1)*9+10
matrix rh2014=J(`NO',34,.)

local i=1
foreach o of global outcomes_3{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=17
	rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off)
	global bw_`r' `e(h_r)'
	global br_`r' `e(b_r)'
	matrix rh2014[`i',`=`j'+1']=e(tau_cl)
	matrix rh2014[`i',`=`j'+2']=e(tau_bc)
	matrix rh2014[`i',`=`j'+3']=e(se_tau_cl)
	matrix rh2014[`i',`=`j'+4']=e(se_tau_rb)
	matrix rh2014[`i',`=`j'+5']=e(ci_r_rb)
	matrix rh2014[`i',`=`j'+6']=e(ci_l_rb)
	matrix rh2014[`i',`=`j'+7']=e(tau_cl_l)
	matrix rh2014[`i',`=`j'+8']=e(tau_bc_l)
	matrix rh2014[`i',`=`j'+9']=e(pv_cl)
	matrix rh2014[`i',`=`j'+10']=e(pv_rb)
	matrix rh2014[`i',`=`j'+11']=e(h_l)
	matrix rh2014[`i',`=`j'+12']=e(b_l)
	matrix rh2014[`i',`=`j'+13']=e(N_h_l)
	matrix rh2014[`i',`=`j'+14']=e(N_h_r)
	matrix rh2014[`i',`=`j'+15']=e(N_b_l)
	matrix rh2014[`i',`=`j'+16']=e(N_b_r)
	matrix rh2014[`i',`=`j'+17']=e(N)
	}	

	local i=1+`i'
	foreach w in hq lq pri pub hq_pri hq_pub lq_pri lq_pub{
		foreach r in saber11 sisben{
		if "`r'"=="saber11" local j=0
		if "`r'"=="sisben" 	local j=17

		cap rdrobust `o'_`w' running_`r' if ${cond_`r'}, masspoints(off) h(${bw_`r'}) b(${br_`r'}) 
		if _rc==0{
		matrix rh2014[`i',`=`j'+1']=e(tau_cl)
		matrix rh2014[`i',`=`j'+2']=e(tau_bc)
		matrix rh2014[`i',`=`j'+3']=e(se_tau_cl)
		matrix rh2014[`i',`=`j'+4']=e(se_tau_rb)
		matrix rh2014[`i',`=`j'+5']=e(ci_r_rb)
		matrix rh2014[`i',`=`j'+6']=e(ci_l_rb)
		matrix rh2014[`i',`=`j'+7']=e(tau_cl_l)
		matrix rh2014[`i',`=`j'+8']=e(tau_bc_l)
		matrix rh2014[`i',`=`j'+9']=e(pv_cl)
		matrix rh2014[`i',`=`j'+10']=e(pv_rb)
		matrix rh2014[`i',`=`j'+11']=e(h_l)
		matrix rh2014[`i',`=`j'+12']=e(b_l)
		matrix rh2014[`i',`=`j'+13']=e(N_h_l)
		matrix rh2014[`i',`=`j'+14']=e(N_h_r)
		matrix rh2014[`i',`=`j'+15']=e(N_b_l)
		matrix rh2014[`i',`=`j'+16']=e(N_b_r)
		matrix rh2014[`i',`=`j'+17']=e(N)
		}
		}
		
		local i=1+`i'
	}
	
	if "`o'"=="graduation_exam" | "`o'"=="graduation_exam_pro"{
		foreach w in stem stem_plus arts social other{
			foreach r in saber11 sisben{
			if "`r'"=="saber11" local j=0
			if "`r'"=="sisben" 	local j=17

			cap noisily rdrobust `o'_`w' running_`r' if ${cond_`r'}, masspoints(off) h(${bw_`r'}) b(${br_`r'}) 
			if _rc==0{
			matrix rh2014[`i',`=`j'+1']=e(tau_cl)
			matrix rh2014[`i',`=`j'+2']=e(tau_bc)
			matrix rh2014[`i',`=`j'+3']=e(se_tau_cl)
			matrix rh2014[`i',`=`j'+4']=e(se_tau_rb)
			matrix rh2014[`i',`=`j'+5']=e(ci_r_rb)
			matrix rh2014[`i',`=`j'+6']=e(ci_l_rb)
			matrix rh2014[`i',`=`j'+7']=e(tau_cl_l)
			matrix rh2014[`i',`=`j'+8']=e(tau_bc_l)
			matrix rh2014[`i',`=`j'+9']=e(pv_cl)
			matrix rh2014[`i',`=`j'+10']=e(pv_rb)
			matrix rh2014[`i',`=`j'+11']=e(h_l)
			matrix rh2014[`i',`=`j'+12']=e(b_l)
			matrix rh2014[`i',`=`j'+13']=e(N_h_l)
			matrix rh2014[`i',`=`j'+14']=e(N_h_r)
			matrix rh2014[`i',`=`j'+15']=e(N_b_l)
			matrix rh2014[`i',`=`j'+16']=e(N_b_r)
			matrix rh2014[`i',`=`j'+17']=e(N)
			}
			}
			
			local i=1+`i'
		}		
	}
}

}
********************************************************************************
*** ESTIMATION 2SLS
********************************************************************************

if `2sls'{
drop costo*_ge
global outcomes_4 access_0 access_0_hq access_0_hq_pri access_6 access_6_hq access_6_hq_pri graduation_exam_7 graduation_exam_pro_7 graduation_exam_pro_7_hq graduation_exam_pro_7_hq_pri score_saberpro score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 work_1 work_2 work_3 work_4 work_5 work_6 work_7 work_8 wage0_1 wage0_2 wage0_3 wage0_4 wage0_5 wage0_6 wage0_7 wage0_8 mw_1 mw_2 mw_3 mw_4 mw_5 mw_6 mw_7 mw_8 ln_wage_1 ln_wage_2 ln_wage_3 ln_wage_4 ln_wage_5 ln_wage_6 ln_wage_7 ln_wage_8 access_pro_0 access_tyt_0 access_pro_6 access_tyt_6 graduation_exam_tyt quarters_worked costo_privado_1 costo_privado_2 costo_privado_3 costo_privado_4 costo_privado_5 costo_privado_6 costo_privado_7 costo_privado_8 costo_social_1 costo_social_2 costo_social_3 costo_social_4 costo_social_5 costo_social_6 costo_social_7 costo_social_8 costo_matricula_1 costo_matricula_2 costo_matricula_3 costo_matricula_4 costo_matricula_5 costo_matricula_6 costo_matricula_pub_1 costo_matricula_pub_2 costo_matricula_pub_3 costo_matricula_pub_4 costo_matricula_pub_5 costo_matricula_pub_6 costo_social_pub_1 costo_social_pub_2 costo_social_pub_3 costo_social_pub_4 costo_social_pub_5 costo_social_pub_6 work_9 mw_9 wage0_9 ln_wage_9 
global iv beneficiary_spp 
 
foreach f of global iv{
	local NO: word count of $outcomes_4
	local NO=`NO'-1
	matrix r2_`f'=J(`NO',42,.)
	local i=1
	foreach o of global outcomes_4{
		foreach r in saber11 sisben{
		if "`r'"=="saber11" local j=0
		if "`r'"=="sisben" 	local j=21
		
		cap rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) fuzzy(`f')
		if _rc==0{
		matrix r2_`f'[`i',`=`j'+1']=e(tau_T_cl)
		matrix r2_`f'[`i',`=`j'+2']=e(tau_T_bc)
		matrix r2_`f'[`i',`=`j'+3']=e(se_tau_T_cl)
		matrix r2_`f'[`i',`=`j'+4']=e(se_tau_T_rb)
		matrix r2_`f'[`i',`=`j'+5']=e(tau_cl)
		matrix r2_`f'[`i',`=`j'+6']=e(tau_bc)
		matrix r2_`f'[`i',`=`j'+7']=e(se_tau_cl)
		matrix r2_`f'[`i',`=`j'+8']=e(se_tau_rb)
		matrix r2_`f'[`i',`=`j'+9']=e(ci_r_rb)
		matrix r2_`f'[`i',`=`j'+10']=e(ci_l_rb)
		matrix r2_`f'[`i',`=`j'+11']=e(tau_cl_l)
		matrix r2_`f'[`i',`=`j'+12']=e(tau_bc_l)
		matrix r2_`f'[`i',`=`j'+13']=e(pv_cl)
		matrix r2_`f'[`i',`=`j'+14']=e(pv_rb)
		matrix r2_`f'[`i',`=`j'+15']=e(h_l)
		matrix r2_`f'[`i',`=`j'+16']=e(b_l)
		matrix r2_`f'[`i',`=`j'+17']=e(N_h_l)
		matrix r2_`f'[`i',`=`j'+18']=e(N_h_r)
		matrix r2_`f'[`i',`=`j'+19']=e(N_b_l)
		matrix r2_`f'[`i',`=`j'+20']=e(N_b_r)
		matrix r2_`f'[`i',`=`j'+21']=e(N)
		}
		else {
			dis as error "`o' `r' `f' could not be run"
		}
		}
		local i=1+`i'
	}
}
}
********************************************************************************
*** ESTIMATION BY HETEROGENEITIES
********************************************************************************

if `heterogeneities'{
	
gen icfes_stratum456=(icfes_stratum4==1 | icfes_stratum5==1 | icfes_stratum6==1)
foreach x in quartil_enrollment_hq college_selectivity major_selectivity{
	replace `x'=0 if `x'==1 | `x'==2
	replace `x'=1 if `x'==3 | `x'==4
}

sum sisben_score if eligible_sisben==1,d
gen median_saber11=(sisben_score > r(p50)) if eligible_sisben==1
sum running_saber11 if eligible_saber11==1,d
gen median_sisben=(running_saber11 > r(p50)) if eligible_saber11==1


global outcomes_5 access_0 access_0_hq access_0_lq graduation_exam graduation_exam_pro graduation_exam_tyt graduation_exam_pro_hq graduation_exam_pro_lq graduation_exam_pro_stem graduation_exam_pro_stem_plus graduation_exam_pro_arts graduation_exam_pro_social graduation_exam_pro_other score_saberpro score_saberpro_3 score_saberpro_4 score_saberpro_5 score_saberpro_6 score_saberpro_7 score_sabertyt postgraduate work_7 work_8 wage0_7 wage0_8 mw_7 mw_8 ln_wage_7 ln_wage_8 work_8 work_9 mw_9 wage0_9 ln_wage_9
global het1 icfes_stratum1 icfes_stratum2 icfes_stratum3 icfes_stratum456
global het2 icfes_female ethnminority student_resides_urban parental_ba quartil_enrollment_hq college_selectivity major_selectivity
global het4 quartil_sb11 quartil_enrollment quartil_p310

foreach o of global outcomes_5{
	matrix `o'=J(17,66,.)
	foreach r in saber11 sisben{
		local i=1
		if "`r'"=="saber11" local j=0
		if "`r'"=="sisben" 	local j=33
		local i=`i'+`j'
		dis " `o' `r' `i'"
		rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) fuzzy(beneficiary_spp)
		global bw_`r' `e(h_r)'
		global br_`r' `e(b_r)'
		matrix `o'[1,`i']=e(tau_cl)
		matrix `o'[2,`i']=e(tau_bc)
		matrix `o'[3,`i']=e(se_tau_cl)
		matrix `o'[4,`i']=e(se_tau_rb)
		matrix `o'[5,`i']=e(ci_r_rb)
		matrix `o'[6,`i']=e(ci_l_rb)
		matrix `o'[7,`i']=e(tau_cl_l)
		matrix `o'[8,`i']=e(tau_bc_l)
		matrix `o'[9,`i']=e(pv_cl)
		matrix `o'[10,`i']=e(pv_rb)
		matrix `o'[11,`i']=e(h_l)
		matrix `o'[12,`i']=e(b_l)
		matrix `o'[13,`i']=e(N_h_l)
		matrix `o'[14,`i']=e(N_h_r)
		matrix `o'[15,`i']=e(N_b_l)
		matrix `o'[16,`i']=e(N_b_r)
		matrix `o'[17,`i']=e(N)
		local i=`i'+1
		
		foreach h of global het1{
			dis " `o' `r' `h' `i'"
			cap rdrobust `o' running_`r' if ${cond_`r'} & `h'==1, masspoints(off) h(${bw_`r'}) b(${br_`r'}) fuzzy(beneficiary_spp)
			if _rc==0{
			matrix `o'[1,`i']=e(tau_cl)
			matrix `o'[2,`i']=e(tau_bc)
			matrix `o'[3,`i']=e(se_tau_cl)
			matrix `o'[4,`i']=e(se_tau_rb)
			matrix `o'[5,`i']=e(ci_r_rb)
			matrix `o'[6,`i']=e(ci_l_rb)
			matrix `o'[7,`i']=e(tau_cl_l)
			matrix `o'[8,`i']=e(tau_bc_l)
			matrix `o'[9,`i']=e(pv_cl)
			matrix `o'[10,`i']=e(pv_rb)
			matrix `o'[11,`i']=e(h_l)
			matrix `o'[12,`i']=e(b_l)
			matrix `o'[13,`i']=e(N_h_l)
			matrix `o'[14,`i']=e(N_h_r)
			matrix `o'[15,`i']=e(N_b_l)
			matrix `o'[16,`i']=e(N_b_r)
			matrix `o'[17,`i']=e(N)
			}
			else {
				dis as error "`o' `h' could not be run on `r'"
			}
			local i=`i'+1
		}
		
		foreach h of global het2{
			forvalues w=0/1{
				dis " `o' `r' `h' `w' `i'"
				cap rdrobust `o' running_`r' if ${cond_`r'} & `h'==`w', masspoints(off) h(${bw_`r'}) b(${br_`r'}) fuzzy(beneficiary_spp)
				if _rc==0{
				matrix `o'[1,`i']=e(tau_cl)
				matrix `o'[2,`i']=e(tau_bc)
				matrix `o'[3,`i']=e(se_tau_cl)
				matrix `o'[4,`i']=e(se_tau_rb)
				matrix `o'[5,`i']=e(ci_r_rb)
				matrix `o'[6,`i']=e(ci_l_rb)
				matrix `o'[7,`i']=e(tau_cl_l)
				matrix `o'[8,`i']=e(tau_bc_l)
				matrix `o'[9,`i']=e(pv_cl)
				matrix `o'[10,`i']=e(pv_rb)
				matrix `o'[11,`i']=e(h_l)
				matrix `o'[12,`i']=e(b_l)
				matrix `o'[13,`i']=e(N_h_l)
				matrix `o'[14,`i']=e(N_h_r)
				matrix `o'[15,`i']=e(N_b_l)
				matrix `o'[16,`i']=e(N_b_r)
				matrix `o'[17,`i']=e(N)
				}
				else {
					dis as error "`o' `h' `w' could not be run on `r'"
				}
				local i=`i'+1
			}
		}
		
		foreach h of global het4{
			forvalues w=1/4{
				dis " `o' `r' `h' `w' `i'"
				cap rdrobust `o' running_`r' if ${cond_`r'} & `h'==`w', masspoints(off) h(${bw_`r'}) b(${br_`r'}) fuzzy(beneficiary_spp)
				if _rc==0{ 
				matrix `o'[1,`i']=e(tau_cl)
				matrix `o'[2,`i']=e(tau_bc)
				matrix `o'[3,`i']=e(se_tau_cl)
				matrix `o'[4,`i']=e(se_tau_rb)
				matrix `o'[5,`i']=e(ci_r_rb)
				matrix `o'[6,`i']=e(ci_l_rb)
				matrix `o'[7,`i']=e(tau_cl_l)
				matrix `o'[8,`i']=e(tau_bc_l)
				matrix `o'[9,`i']=e(pv_cl)
				matrix `o'[10,`i']=e(pv_rb)
				matrix `o'[11,`i']=e(h_l)
				matrix `o'[12,`i']=e(b_l)
				matrix `o'[13,`i']=e(N_h_l)
				matrix `o'[14,`i']=e(N_h_r)
				matrix `o'[15,`i']=e(N_b_l)
				matrix `o'[16,`i']=e(N_b_r)
				matrix `o'[17,`i']=e(N)
				}
				else {
					dis as error "`o' `h' `w' could not be run on `r'"
				}					
				local i=`i'+1
			}
		}
	
		/*Terciles*/
		forvalues w=0/1{
			dis " `o' `r' tercil `w' `i'"
			cap rdrobust `o' running_`r' if ${cond_`r'} & median_`r'==`w', masspoints(off) h(${bw_`r'}) b(${br_`r'}) fuzzy(beneficiary_spp)
			if _rc==0{ 
			matrix `o'[1,`i']=e(tau_cl)
			matrix `o'[2,`i']=e(tau_bc)
			matrix `o'[3,`i']=e(se_tau_cl)
			matrix `o'[4,`i']=e(se_tau_rb)
			matrix `o'[5,`i']=e(ci_r_rb)
			matrix `o'[6,`i']=e(ci_l_rb)
			matrix `o'[7,`i']=e(tau_cl_l)
			matrix `o'[8,`i']=e(tau_bc_l)
			matrix `o'[9,`i']=e(pv_cl)
			matrix `o'[10,`i']=e(pv_rb)
			matrix `o'[11,`i']=e(h_l)
			matrix `o'[12,`i']=e(b_l)
			matrix `o'[13,`i']=e(N_h_l)
			matrix `o'[14,`i']=e(N_h_r)
			matrix `o'[15,`i']=e(N_b_l)
			matrix `o'[16,`i']=e(N_b_r)
			matrix `o'[17,`i']=e(N)
			}
			else {
				dis as error "`o' tercil `w' could not be run on `r'"
			}					
			local i=`i'+1
			
		}
	}
}
}
***********************************
** Raw tables
***********************************	

if `rd'{
clear
svmat r2014
gen nombre=""
local NO: word count of $outcomes
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${outcomes}
	replace nombre="`nn'" in `i'
}
order nombre
local nombres cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
local nn: word count of `nombres'
local nn=`nn'-1
forvalues j=1/`nn'{
	local rn: word `j' of `nombres'
	rename r2014`j' `rn'_11
	rename r2014`=`j'+`nn'' `rn'_si
}

local runn 11 si
foreach x of local runn{
	label var cc_`x' "Coeficiente convencional"
	label var cr_`x' "Coeficiente robusto"
	label var sec_`x' "Error estándar convencional"
	label var ser_`x' "Error estándar robusto"
	label var cs_`x' "Intervalo superior robusto"
	label var ci_`x' "Intervalo inferior robusto"
	label var cc0_`x' "Media control convencional"
	label var cr0_`x' "Media control robusto"
	label var pc_`x' "Valor P convencional"
	label var pr_`x' "Valor P robusto"
	label var bwc_`x' "BW convencional"
	label var bwr_`x' "BW robusto"
	label var nec0_`x' "Observaciones efectivas izquierda convencional"
	label var nec_`x' "Observaciones efectivas derecha convencional"
	label var ner0_`x' "Observaciones efectivas izquierda robustas"
	label var ner_`x' "Observaciones efectivas derecha robustas"
	label var n_`x' "Observaciones"
}

save "${tables}/Resultados_rd", replace
}
********************************************************************************
if `years'{
clear
svmat ry2014
gen nombre=""
local NO: word count of $outcomes_2
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${outcomes_2}
	replace nombre="`nn'" in `i'
}
order nombre
local nombres cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
local nn: word count of `nombres'
local nn=`nn'-1
forvalues j=1/`nn'{
	local rn: word `j' of `nombres'
	rename ry2014`j' `rn'_11
	rename ry2014`=`j'+`nn'' `rn'_si
}

local runn 11 si
foreach x of local runn{
	label var cc_`x' "Coeficiente convencional"
	label var cr_`x' "Coeficiente robusto"
	label var sec_`x' "Error estándar convencional"
	label var ser_`x' "Error estándar robusto"
	label var cs_`x' "Intervalo superior robusto"
	label var ci_`x' "Intervalo inferior robusto"
	label var cc0_`x' "Media control convencional"
	label var cr0_`x' "Media control robusto"
	label var pc_`x' "Valor P convencional"
	label var pr_`x' "Valor P robusto"
	label var bwc_`x' "BW convencional"
	label var bwr_`x' "BW robusto"
	label var nec0_`x' "Observaciones efectivas izquierda convencional"
	label var nec_`x' "Observaciones efectivas derecha convencional"
	label var ner0_`x' "Observaciones efectivas izquierda robustas"
	label var ner_`x' "Observaciones efectivas derecha robustas"
	label var n_`x' "Observaciones"
}

save "${tables}/Resultados_years", replace
}
********************************************************************************
if `het'{
clear
svmat rh2014
gen nombre=""
global nombres 
foreach x of global outcomes_3{
	global nombres ${nombres} `x'
	foreach w in hq lq pri pub hq_pri hq_pub lq_pri lq_pub{
		global nombres ${nombres} `x'_`w'
	}
	if "`x'"=="graduation_exam" | "`x'"=="graduation_exam_pro"{
		foreach w in stem stem_plus arts social other{
			global nombres ${nombres} `x'_`w'
		}
	}
}

local NO: word count of $nombres
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${nombres}
	replace nombre="`nn'" in `i'
}
order nombre
local nombres cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
local nn: word count of `nombres'
local nn=`nn'-1
forvalues j=1/`nn'{
	local rn: word `j' of `nombres'
	rename rh2014`j' `rn'_11
	rename rh2014`=`j'+`nn'' `rn'_si
}

local runn 11 si
foreach x of local runn{
	label var cc_`x' "Coeficiente convencional"
	label var cr_`x' "Coeficiente robusto"
	label var sec_`x' "Error estándar convencional"
	label var ser_`x' "Error estándar robusto"
	label var cs_`x' "Intervalo superior robusto"
	label var ci_`x' "Intervalo inferior robusto"
	label var cc0_`x' "Media control convencional"
	label var cr0_`x' "Media control robusto"
	label var pc_`x' "Valor P convencional"
	label var pr_`x' "Valor P robusto"
	label var bwc_`x' "BW convencional"
	label var bwr_`x' "BW robusto"
	label var nec0_`x' "Observaciones efectivas izquierda convencional"
	label var nec_`x' "Observaciones efectivas derecha convencional"
	label var ner0_`x' "Observaciones efectivas izquierda robustas"
	label var ner_`x' "Observaciones efectivas derecha robustas"
	label var n_`x' "Observaciones"
}

save "${tables}/Resultados_het", replace
}
********************************************************************************
if `2sls'{
foreach f of global iv{
	clear
	svmat r2_`f'
	gen nombre=""
	local NO: word count of $outcomes_4
	local NO=`NO'-1
	forvalues i=1/`NO'{
		local nn: word `i' of ${outcomes_4}
		replace nombre="`nn'" in `i'
	}
	order nombre
	local nombres cc1 cr1 sec1 ser1 cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
	local nn: word count of `nombres'
	local nn=`nn'-1
	forvalues j=1/`nn'{
		local rn: word `j' of `nombres'
		rename r2_`f'`j' `rn'_11
		rename r2_`f'`=`j'+`nn'' `rn'_si
	}

	local runn 11 si
	foreach x of local runn{
		label var cc1_`x' "Coeficiente convencional 1º etapa"
		label var cr1_`x' "Coeficiente robusto 1º etapa"
		label var sec1_`x' "Error estándar convencional 1º etapa"
		label var ser1_`x' "Error estándar robusto 1º etapa"
		label var cc_`x' "Coeficiente convencional"
		label var cr_`x' "Coeficiente robusto"
		label var sec_`x' "Error estándar convencional"
		label var ser_`x' "Error estándar robusto"
		label var cs_`x' "Intervalo superior robusto"
		label var ci_`x' "Intervalo inferior robusto"
		label var cc0_`x' "Media control convencional"
		label var cr0_`x' "Media control robusto"
		label var pc_`x' "Valor P convencional"
		label var pr_`x' "Valor P robusto"
		label var bwc_`x' "BW convencional"
		label var bwr_`x' "BW robusto"
		label var nec0_`x' "Observaciones efectivas izquierda convencional"
		label var nec_`x' "Observaciones efectivas derecha convencional"
		label var ner0_`x' "Observaciones efectivas izquierda robustas"
		label var ner_`x' "Observaciones efectivas derecha robustas"
		label var n_`x' "Observaciones"
	}

	save "${tables}/Resultados_2sls_`f'", replace
}
}
********************************************************************************
if `heterogeneities'{
foreach f of global outcomes_5{
	clear
	svmat `f'
	gen nombre=""
	replace nombre= "Coeficiente convencional" in 1
	replace nombre= "Coeficiente robusto" in 2
	replace nombre= "Error estándar convencional" in 3
	replace nombre= "Error estándar robusto" in 4
	replace nombre= "Intervalo superior robusto" in 5 
	replace nombre= "Intervalo inferior robusto" in 6
	replace nombre= "Media control convencional" in 7
	replace nombre= "Media control robusto" in 8
	replace nombre= "Valor P convencional" in 9
	replace nombre= "Valor P robusto" in 10
	replace nombre= "BW convencional" in 11
	replace nombre= "BW robusto" in 12
	replace nombre= "Observaciones efectivas izquierda convencional" in 13
	replace nombre= "Observaciones efectivas derecha convencional" in 14
	replace nombre= "Observaciones efectivas izquierda robustas" in 15
	replace nombre= "Observaciones efectivas derecha robustas" in 16
	replace nombre= "Observaciones" in 17

	foreach x in saber11 sisben{
		if "`x'"=="saber11" local j=0
		if "`x'"=="sisben" 	local j=33
		             
		rename `f'`=`j'+1' total_`x'
		rename `f'`=`j'+2' stratum_1_`x'
		rename `f'`=`j'+3' stratum_2_`x'
		rename `f'`=`j'+4' stratum_3_`x'
		rename `f'`=`j'+5' stratum_456_`x'
		rename `f'`=`j'+6' male_`x'
		rename `f'`=`j'+7' female_`x'
		rename `f'`=`j'+8' noethnia_`x'
		rename `f'`=`j'+9' ethnia_`x'
		rename `f'`=`j'+10' rural_`x'
		rename `f'`=`j'+11' urban_`x'
		rename `f'`=`j'+12' noparental_ba_`x'
		rename `f'`=`j'+13' parental_ba_`x'
		rename `f'`=`j'+14' enrollment_hq_1_`x'
		rename `f'`=`j'+15' enrollment_hq_2_`x'
		rename `f'`=`j'+16' college_selectivity_1_`x'
		rename `f'`=`j'+17' college_selectivity_2_`x'
		rename `f'`=`j'+18' major_selectivity_1_`x'
		rename `f'`=`j'+19' major_selectivity_2_`x'
		rename `f'`=`j'+20' quartil_sb11_1_`x'
		rename `f'`=`j'+21' quartil_sb11_2_`x'
		rename `f'`=`j'+22' quartil_sb11_3_`x'
		rename `f'`=`j'+23' quartil_sb11_4_`x'
		rename `f'`=`j'+24' quartil_enrollment_1_`x'
		rename `f'`=`j'+25' quartil_enrollment_2_`x'
		rename `f'`=`j'+26' quartil_enrollment_3_`x'
		rename `f'`=`j'+27' quartil_enrollment_4_`x'
		rename `f'`=`j'+28' quartil_p310_1_`x'
		rename `f'`=`j'+29' quartil_p310_2_`x'
		rename `f'`=`j'+30' quartil_p310_3_`x'
		rename `f'`=`j'+31' quartil_p310_4_`x'		
		rename `f'`=`j'+32' below_median_`x'
		rename `f'`=`j'+33' above_median_`x'
	}
	
	order nombre
	
	save "${tables}/heterogeneities_`f'", replace
}
}

